<?php

/**
 * This is the model class for table "vendors".
 *
 * The followings are the available columns in table 'vendors':
 * @property integer $id
 * @property integer $organization_id
 * @property string $name
 * @property string $address
 * @property string $address2
 * @property string $city
 * @property integer $state_id
 * @property string $zip_code
 * @property string $phone
 * @property string $contact_first_name
 * @property string $contact_last_name
 * @property string $contact_home_phone
 * @property string $contact_mobile_phone
 * @property string $contact_email
 */
class Vendor extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Vendor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'vendors';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('organization_id, name, address, city, state_id, zip_code, phone, contact_email', 'required'),
			array('organization_id, state_id', 'numerical', 'integerOnly'=>true),
			array('name, address, address2, contact_first_name, contact_last_name, contact_email', 'length', 'max'=>50),
			array('city', 'length', 'max'=>25),
			array('zip_code', 'length', 'max'=>10),
			array('phone', 'length', 'max'=>15),
			array('contact_home_phone, contact_mobile_phone', 'length', 'max'=>12),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, organization_id, name, address, address2, city, state_id, zip_code, phone, contact_first_name, contact_last_name, contact_home_phone, contact_mobile_phone, contact_email', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'state' => array(self::BELONGS_TO, 'State', 'state_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'organization_id' => 'Organization',
			'name' => 'Name',
			'address' => 'Address',
			'address2' => 'Address2',
			'city' => 'City',
			'state_id' => 'State',
			'zip_code' => 'Zip Code',
			'phone' => 'Phone',
			'contact_first_name' => 'Contact First Name',
			'contact_last_name' => 'Contact Last Name',
			'contact_home_phone' => 'Contact Home Phone',
			'contact_mobile_phone' => 'Contact Mobile Phone',
			'contact_email' => 'Contact Email',
		);
	}
	
	public function getContactName()
	{
		if (!empty($this->contact_first_name) || !empty($this->contact_last_name))
		{
			
			// if both first and last name are present
			if (!empty($this->contact_first_name) && !empty($this->contact_last_name))
			{
				$contact = "{$this->contact_last_name}, {$this->contact_first_name}";
			}
			// if only first name is present
			elseif (!empty($this->contact_first_name) && empty($this->contact_last_name)) {
				$contact = $this->contact_first_name;
			}
			// if only last name is present
			elseif (empty($this->contact_first_name) && !empty($this->contact_last_name)) {
				$contact = $this->contact_last_name;
			}

		}
		else {
			$contact = $this->contact_email;
		}
		
		return $contact;
	}
	
	public function behaviors()
	{ 
		return array( 'LoggableBehavior'=> 'application.modules.auditTrail.behaviors.LoggableBehavior', ); 
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('organization_id',$this->organization_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('address2',$this->address2,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('state_id',$this->state_id);
		$criteria->compare('zip_code',$this->zip_code,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('contact_first_name',$this->contact_first_name,true);
		$criteria->compare('contact_last_name',$this->contact_last_name,true);
		$criteria->compare('contact_home_phone',$this->contact_home_phone,true);
		$criteria->compare('contact_mobile_phone',$this->contact_mobile_phone,true);
		$criteria->compare('contact_email',$this->contact_email,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}